<template>
	<view class='content'>
        <!-- #ifdef MP-WEIXIN -->
        <top-nav :inner-title="title" is-show-back="true" is-fixed="true" :navH="navH" :n="n" is-white="bg-white" opacity="1"></top-nav>
        <view  :style="{height:navH + 'px'}"></view>
        <!-- #endif -->  
		<block v-if='list.length > 0'>
			<view class="item_out">
				<block v-for='(item,index) in list' :key="index">
					<view class='item'>
						<view class="address_left" @click='active(item.id)'>
							<view class='active'>
								<view :class='item.is_active == 1 ? "ck":""'></view>
							</view>
							<view class='info' >
								<block v-if="item.school_type == 1">
									<view class='address'>{{item.school_name}} {{item.addr_name}} {{item.num}}</view>
								</block>
								<block v-else>
									<view class='address'>{{item.addr_name}} {{item.num}}</view>
								</block>
								<view class='my'>
									<text class='name'>{{item.name}}</text>
									<text class='sex'>{{item.sex == 1?"男同学":"女同学"}}</text>
									<view class='phone'>{{item.phone}}</view>
								</view>
							</view>
						</view>
						
						<view class='icon' @click='edit(item.id)'>
							<icon></icon>
						</view>
					</view>
				</block>
			</view>
		</block>
		<block v-else>
			<view class='empty'>
				<view class='img'></view>
				<text>您还没有添加收货地址哦</text>
			</view>
		</block>
		  <view class='button'  @click='addAddress()'>
			<view class='icon'></view>
			<view class='text'>新增收货地址</view>
		  </view>
	</view>
</template>

<script>
	import uniFly from 'unifly';
	import request from '../../common/request.js';
	// import $api from '../../api/api.js';
	import $api_base from '../../api/api_base.js';
	import topNav from '@/template/topnav/index.vue';
	import event from '../../common/event.js';
	export default {
		components: { topNav },
		data() {
			return {
				list: [],
				navH:0,
				n:0,
				title: '我的收货地址',
				fromPage: ''
			}
		},
		onLoad: function(options){
			var fromPage = options.fromPage || '';
			this.fromPage = fromPage;
            this.navH = getApp().globalData.navHeight; 
			this.n = getApp().globalData.n;
		 },
		   
		   
		onShow: function (options) {
			this.getAddress();
		 },
		 methods:{
			 active: function (e) {
				var id = e;
				var self = this;
				var fromPage = self.fromPage;
				// $api.activeAddress({ id: id }).then(function(res){
				// 	let data = res.data;
				//   self.getAddress();
				// });
				$api_base.post($api_base.activeAddress,{id: id }, 1).then(function(res){
					let data = res.data;
					self.getAddress();
				});
				if (fromPage == 'buy') {
					setTimeout(function () {
						uni.navigateBack();
					}, 200)
				}
			  },
			  getAddress: function(){
					var self = this;
					$api_base.post($api_base.getAddress,{},1).then(function(res){
						let data = res.data;
						self.list = data.list 
					})
			  },
			  edit: function (e) {
				var id = e;
				uni.navigateTo({ url: '/pagesBuy/address-edit/index?id=' + id });
			  },
			  addAddress: function () {
				if (this.list.length >= 5) {
				  uni.showToast({ title: "最多只能添加5个", icon: "none" });
				  return false;
				}
				uni.navigateTo({ url: '/pagesBuy/address-edit/index' })
			  }
		 }
		
	}
</script>
<style>
  page {
  background-color: #fff;
  }
</style>
<style lang="scss" scoped>
	@import 'index.scss';
</style>
